<?php

namespace slinks\common\io;

/**
 * A class visitor that works with the Annotaion Reader
 * @author navid
 *
 */
use slinks\common\addendum\ReflectionAnnotatedClass;

class AnnotationClassVisitor implements IClassVisitor {
    /* @var $annotationVisitor IAnnotationVisitor */

    private $annotationVisitor = null;
    private $skipErrors = false;

    public function __construct(IAnnotationVisitor $annotationVisitor) {
        $this->annotationVisitor = $annotationVisitor;
    }

    /**
     * This is called by the ClassReader when it reaches a new Class or Interface in the filesytem.
     * @param string $info the SplFileInfo for the file containing the class.
     * @param string $name the name of the class being visited 
     */
    public function visit(\SplFileInfo $info, $name) {
        try {

            $reflection = new ReflectionAnnotatedClass($name);
            if (count($reflection->getAnnotations()) > 0) {
                $this->annotationVisitor->visit($reflection);
            }
        } catch (Exception $e) {
            if (!$this->skipErrors) {
                throw $e;
            }
        }
    }

    /**
     * @return bool true if any errors encountered while reading annotations from the visited classes. 
     */
    public function isSkipErrors() {
        return $this->skipErrors;
    }

    /**
     * Set if errors should be skipped while reading annotations from the visited classes. 
     * @param bool $skipErrors true to skip any errors encountered while reading annotations from the visited classes.  Default is false.
     */
    public function setSkipErrors($skipErrors) {
        $this->skipErrors = $skipErrors;
    }

}

